package nagel_schreckenberg;

import java.util.ArrayList;
import java.util.Random;

import nagel_schreckenberg.Lane.Direction;

public class CarFactory implements Intersection{

	static int counter = 0;
	private double spawnProb;
	private Random rnd;
	private Lane target;
	private boolean spawned;
	
	public enum Placement{
		NORTH, EAST, SOUTH, WEST;
	}
	private Placement p;
	
	public CarFactory(){
		rnd = new Random();
		spawned = false;
		spawnProb = Config.SPAWN;
	}
	
	public void addLanes(Lane l){
		target = l;
		l.connectSource(this);
	}
	
	@Override
	public void spawn(){
		double sp = rnd.nextDouble();
		spawned = false;
		if(sp < spawnProb){
		int speed = rnd.nextInt(target.getSpeedLimit()+1);
		Vehicle v = new AverageCar(counter, speed);
		if(target.getVehicles().isEmpty()){
			counter++;
			v.setPosition(0);
			target.addVehicle(v);
			spawned = true;
		}
		if(target.getVehicles().get(target.getVehicles().size()-1).getPosition() != 0){
			counter++;
			v.setPosition(0);
			target.addVehicle(v);
			spawned = true;
		}
		}
	}

	@Override
	public Lane getIn(Direction d) {return null;}

	@Override
	public void clearSink() {}

	@Override
	public Lane getOut(Direction d) {return null;}

	@Override
	public void controlLights() {}

	@Override
	public boolean hasSpawned() {
		boolean isit = spawned;
		spawned = false;
		return isit;
	}

	@Override
	public void gershenson() {}

	@Override
	public int countSink() {return 0;}
	
	public Placement getPlacement(){
		return p;
	}
	
	public void setPlacement(Placement pl){
		p = pl;
	}
	
	public void setSpawnProb(double d){
		spawnProb = d;
	}

	@Override
	public double travelTime() {return 0;}

	@Override
	public void learning() {}

	@Override
	public void greedy() {}

}
